package br.com.rafsoft.mapaentrega.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import br.com.rafsoft.mapaentrega.AccountsActivity;
import br.com.rafsoft.mapaentrega.MapaEntregaApplication;
import br.com.rafsoft.mapaentrega.R;

public class LoginActivity extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		//final Button button = (Button) findViewById(R.id.buttonLogin);	

	}
	
	public void onRegister(View v) {
		Intent intent = new Intent(LoginActivity.this,AccountsActivity.class);
		startActivity(intent);	
	}
	
	public void onLoginClick(View v) {
		
		final EditText user = (EditText) findViewById(R.id.editTextUserName);
		//final EditText password = (EditText) findViewById(R.id.editTextPassword);
		
		MapaEntregaApplication application = (MapaEntregaApplication) getApplication();
		application.setUser(user.getText().toString());
		application.setUserName(user.getText().toString());

		Intent intent = new Intent(LoginActivity.this,MainActivity.class);
		startActivity(intent);
		
		/*
		new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				MapaEntregaRequestFactory factory = (MapaEntregaRequestFactory) Util
						.getRequestFactory(LoginActivity.this,
								MapaEntregaRequestFactory.class);
				UserRequest request = factory.userRequest();

				final EditText user = (EditText) findViewById(R.id.editTextUserName);
				final EditText password = (EditText) findViewById(R.id.editTextPassword);

				request.login(user.getText().toString(),
						password.getText().toString()).fire(
						new Receiver<UserProxy>() {

							@Override
							public void onSuccess(UserProxy user) {

								//verifica o retorno
								if (user != null) {
									MapaEntregaApplication application = (MapaEntregaApplication) getApplication();
									application.setUser(user.getUserName());
									application.setUserName(user.getName());

									Intent intent = new Intent(LoginActivity.this,MainActivity.class);
									startActivity(intent);
								} else {
									Intent intent = new Intent(LoginActivity.this,MainActivity.class);
									startActivity(intent);
								}

							}
						});

				return null;
			}

		};
		
*/
	}
	
}
